package com.taptech.doufu.ui.view.topicview;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.DFHomeNovelBeans;
import com.taptech.doufu.bean.DFHomeNovelContentBeans;
import com.taptech.doufu.bean.cp.ResponseCpIndexDataBean;
import com.taptech.doufu.ui.view.topicview.adapter.BookListAdapter;
import com.taptech.doufu.util.DiaobaoUtil;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.List;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class NovelTopicBookListView extends LinearLayout{
    private BookListAdapter adapter;
    private TextView tvTitle;
    private View llMore;
    private String moreUrl;
    private String title;
    private List<DFHomeNovelBeans> list;
    public NovelTopicBookListView(Context context) {
        super(context);
    }

    public NovelTopicBookListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NovelTopicBookListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private View root_layout;
    private View linearLayout0;
    private TextView tv_more;
    private View topSpacView;
    private Context context;

    public NovelTopicBookListView(final Context context, final DFHomeNovelContentBeans model, final String type,String pageType){
        this(context);
        View view = LayoutInflater.from(context).inflate(R.layout.topic_nomal_view, null);
        this.context = context;
        linearLayout0 = view.findViewById(R.id.home_dfnovel_layout0);
        topSpacView = view.findViewById(R.id.home_dfnovel_layout_space);
        tv_more = (TextView) view.findViewById(R.id.tv_more);
        root_layout = view.findViewById(R.id.root_layout);
        tvTitle = (TextView) view.findViewById(R.id.tvTitle);
        llMore = view.findViewById(R.id.llMore);
        tvTitle.setText(model.getTitle());
        moreUrl = model.getMore_url();
        title = model.getTitle();
        changeDayModelBg();
        list = model.getList();
        llMore.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                UmengEventUtil.addRightTopBtnEvent(context,model.getEventData(),type,pageType);
                if(moreUrl!=null && moreUrl.contains("/weex/")){
                    SimpleWeexActivity.startActivity(context,moreUrl);
                }else{

                }
            }
        });
        if(!TextUtils.isEmpty(model.getMore_url())){
            llMore.setVisibility(VISIBLE);
        }else{
            llMore.setVisibility(GONE);
        }
        RecyclerView listView = (RecyclerView) view.findViewById(R.id.listView);
        MeasuredLinearLayoutManager manager = new MeasuredLinearLayoutManager(context);
        manager.setScrollEnabled(false);
        adapter = new BookListAdapter(context, list,type,model.getEventData(),pageType);
        listView.setLayoutManager(manager);
        listView.setAdapter(adapter);
        addView(view);
    }

    public void refrush(ResponseCpIndexDataBean.DataBean.ListBean model) {
        if (model == null|| !TextUtils.isEmpty(model.getTitle()) && model.getTitle().equals(title)) {
            return;
        }
        moreUrl = model.getMore_url();
        title = model.getTitle();
        try {
            JSONArray jsonArray = new JSONArray(model.getList().toString());
            list = DiaobaoUtil.json2list(DFHomeNovelBeans.class, jsonArray);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (llMore != null) {
            if(!TextUtils.isEmpty(model.getMore_url())){
                llMore.setVisibility(VISIBLE);
            }else{
                llMore.setVisibility(GONE);
            }
        }
        if (tvTitle != null) {
            tvTitle.setText(model.getTitle());
        }
        if (adapter != null) {
            adapter.refrush(list);
        }
    }


    public void changeDayModelBg(){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg();
        }else {
            dayBg();
        }
    }


    private void dayBg() {
        root_layout.setBackgroundColor(context.getResources().getColor(R.color.home_list_item_color));
        linearLayout0.setBackgroundColor(context.getResources().getColor(R.color.white));
        tvTitle.setTextColor(context.getResources().getColor(R.color.text_color_8));
        tv_more.setTextColor(context.getResources().getColor(R.color.text_color_7));
        topSpacView.setBackgroundColor(context.getResources().getColor(R.color.all_activity_bg));
        tv_more.setTextColor(context.getResources().getColor(R.color.text_color_7));
    }

    private void darkBg() {
        root_layout.setBackgroundColor(context.getResources().getColor(R.color.bg_dark));
        linearLayout0.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        tvTitle.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        tv_more.setTextColor(context.getResources().getColor(R.color.text_dark));
        topSpacView.setBackgroundColor(context.getResources().getColor(R.color.bg_dark));
        tv_more.setTextColor(context.getResources().getColor(R.color.text_dark));
    }
}
